package books.new_to_old

import java.io.BufferedReader
import java.io.File
import java.io.InputStream
import java.io.InputStreamReader
import java.net.URL
import kotlin.concurrent.thread

fun main(args: Array<String>) {
    val file = File("/home/super/IdeaProjects/KotlinLearning/src/main/kotlin/books/new_to_old/KtAndJava.kt")
    //读取整篇文本的内容,.默认Charsets.UTF_8 ,可以通过Charsets.forName("UTF-8")设置字符编码
    println(file.readText())

    //读取每行文本内容,换行符\n
//    println(file.readLines()[0])

    //读取整个文件的字节数组
    println(String(file.readBytes()))

    //kt转java inputStream和bufferReader
    val reader: InputStreamReader = file.reader()
    val inputStream: InputStream = file.inputStream()
    val bufferedReader: BufferedReader = file.bufferedReader()

    //写入一个文本
    //覆盖
//    val writeText = File("/home/super/IdeaProjects/KotlinLearning/src/main/kotlin/books/new_to_old/testWriteText")
//    writeText.writeText("HelloWorld")

    //追加
//    writeText.appendText("next...")
//
//    writeText.appendBytes("\nfinally".toByteArray())

    //遍历
    val dir = File("/home/super/IdeaProjects/KotlinLearning/src/main/kotlin/books/new_to_old")
//    dir.walk().iterator().forEach{
//        println(it.name)
//    }
    dir.walk().filter {
        it.name.endsWith("png")
    }.iterator().forEach { println(it.name) }

//    val srcFile = File("/home/super/IdeaProjects/KotlinLearning/src/main/kotlin/books/new_to_old/@Target元注解.png")
//    val destFile = File("/home/super/IdeaProjects/KotlinLearning/src/main/kotlin/books/test_copy.png")
//    srcFile.copyTo(destFile, true)

    //目录下文件复制到其他目录下
//    val destDir = File("/home/super/IdeaProjects/KotlinLearning/src/main/kotlin/books/test_copy")
//    dir.copyRecursively(destDir, true)


    //获取网络上的文本
    val url = URL("http://www.baidu.com")
//    url.readText().also(::println)
    String(url.readBytes()).also(::println)
//    File(dir, "test").writeBytes(url.readBytes())

}